package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.SystemExample;
import com.cskaoyan.bean.vo.ConfigExpressVO;
import com.cskaoyan.bean.vo.ConfigMallVO;
import com.cskaoyan.bean.vo.ConfigOrderVO;
import com.cskaoyan.bean.vo.ConfigWxVO;
import com.cskaoyan.dao.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.bean.pojo.System;

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 回显及更新商场 运费 订单 微信的配置
 *
 * @author: 程凯迪
 * @class: ConfigMallService
 * @return ConfigMallVO
 */
@SuppressWarnings("all")
@Service
public class ConfigMallServiceImpl implements ConfigMallService {
    @Autowired
    private SystemMapper systemMapper;

    @Override
    public ConfigMallVO configMall() {
        ConfigMallVO configMallVO = new ConfigMallVO();
        configMallVO.setCskaoyanmall_mall_name(getSystemExample("cskaoyan_mall_mall_name").getKeyValue());
        configMallVO.setCskaoyanmall_mall_qq(getSystemExample("cskaoyan_mall_mall_qq").getKeyValue());
        configMallVO.setCskaoyanmall_mall_address(getSystemExample("cskaoyan_mall_mall_address").getKeyValue());
        configMallVO.setCskaoyanmall_mall_phone(getSystemExample("cskaoyan_mall_mall_phone").getKeyValue());
        return configMallVO;
    }

    @Override
    public Integer updateConfigMall(ConfigMallVO configMallbo) {
        updateConfigMall("cskaoyan_mall_mall_name", configMallbo.getCskaoyanmall_mall_name());
        updateConfigMall("cskaoyan_mall_mall_qq", configMallbo.getCskaoyanmall_mall_qq());
        updateConfigMall("cskaoyan_mall_mall_address", configMallbo.getCskaoyanmall_mall_address());
        updateConfigMall("cskaoyan_mall_mall_phone", configMallbo.getCskaoyanmall_mall_phone());
        return 0;
    }

    @Override
    public ConfigExpressVO configExpress() {
        ConfigExpressVO configExpressVO = new ConfigExpressVO();
        configExpressVO.setCskaoyanmall_express_freight_min(getSystemExample("cskaoyan_mall_express_freight_min").getKeyValue());
        configExpressVO.setCskaoyanmall_express_freight_value(getSystemExample("cskaoyan_mall_express_freight_value").getKeyValue());
        return configExpressVO;
    }

    @Override
    public void updateConfigExpress(ConfigExpressVO configExpressVO) {
        updateConfigMall("cskaoyan_mall_express_freight_min", configExpressVO.getCskaoyanmall_express_freight_min());
        updateConfigMall("cskaoyan_mall_express_freight_value", configExpressVO.getCskaoyanmall_express_freight_value());
    }

    @Override
    public ConfigOrderVO configOrder() {
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        configOrderVO.setCskaoyanmall_order_unconfirm(getSystemExample("cskaoyan_mall_order_unconfirm").getKeyValue());
        configOrderVO.setCskaoyanmall_order_unpaid(getSystemExample("cskaoyan_mall_order_unpaid").getKeyValue());
        configOrderVO.setCskaoyanmall_order_comment(getSystemExample("cskaoyan_mall_order_comment").getKeyValue());
        return configOrderVO;
    }

    @Override
    public void updateConfigOrder(ConfigOrderVO configOrderVO) {
        updateConfigMall("cskaoyan_mall_order_unconfirm", configOrderVO.getCskaoyanmall_order_unconfirm());
        updateConfigMall("cskaoyan_mall_order_unpaid", configOrderVO.getCskaoyanmall_order_unpaid());
        updateConfigMall("cskaoyan_mall_order_comment", configOrderVO.getCskaoyanmall_order_comment());
    }

    @Override
    public ConfigWxVO configWx() {
        ConfigWxVO configWxVO = new ConfigWxVO();
        configWxVO.setCskaoyanmall_wx_share(getSystemExample("cskaoyan_mall_wx_share").getKeyValue());
        configWxVO.setCskaoyanmall_wx_index_brand(getSystemExample("cskaoyan_mall_wx_index_brand").getKeyValue());
        configWxVO.setCskaoyanmall_wx_index_topic(getSystemExample("cskaoyan_mall_wx_index_topic").getKeyValue());
        configWxVO.setCskaoyanmall_wx_index_hot(getSystemExample("cskaoyan_mall_wx_index_hot").getKeyValue());
        configWxVO.setCskaoyanmall_wx_catlog_goods(getSystemExample("cskaoyan_mall_wx_catlog_goods").getKeyValue());
        configWxVO.setCskaoyanmall_wx_catlog_list(getSystemExample("cskaoyan_mall_wx_catlog_list").getKeyValue());
        configWxVO.setCskaoyanmall_wx_index_new(getSystemExample("cskaoyan_mall_wx_index_new").getKeyValue());
        return configWxVO;
    }

    @Override
    public void updateConfigWx(ConfigWxVO configWxVO) {
        updateConfigMall("cskaoyan_mall_wx_share", configWxVO.getCskaoyanmall_wx_share());
        updateConfigMall("cskaoyan_mall_wx_index_brand", configWxVO.getCskaoyanmall_wx_index_brand());
        updateConfigMall("cskaoyan_mall_wx_index_topic", configWxVO.getCskaoyanmall_wx_index_topic());
        updateConfigMall("cskaoyan_mall_wx_index_hot", configWxVO.getCskaoyanmall_wx_index_hot());
        updateConfigMall("cskaoyan_mall_wx_catlog_goods", configWxVO.getCskaoyanmall_wx_catlog_goods());
        updateConfigMall("cskaoyan_mall_wx_catlog_list", configWxVO.getCskaoyanmall_wx_catlog_list());
        updateConfigMall("cskaoyan_mall_wx_index_new", configWxVO.getCskaoyanmall_wx_index_new());
    }

    private void updateConfigMall(String keyName, String keyValue) {
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameEqualTo(keyName);
        System system = new System();
        system.setKeyValue(keyValue);
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();// 获取当前时间
        system.setUpdateTime(date);
        systemMapper.updateByExampleSelective(system, systemExample);
    }

    private System getSystemExample(String keyName) {
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameEqualTo(keyName);
        List<System> systems = systemMapper.selectByExample(systemExample);
        //java.lang.System.out.println(systems.get(0));
        return systems.get(0);
    }

}
